package org.lgiavedoni.android.franq.command.system;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.lgiavedoni.android.franq.core.command.Command;
import org.lgiavedoni.android.franq.core.speak.CommonPhraseHelper;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.sync.SyncService;

public class SyncCommand extends Command {

	private static final String OPTION_FORCE = "force";

	@Override
	public void setOptions(Options options) {
		options.addOption(OPTION_FORCE, false, "Force a new synchronization for all the items");
	}

	@Override
	public boolean execute(CommandLine cmd) throws Exception {
		if (SyncService.inst.isStarted()) {
			if (cmd.hasOption(OPTION_FORCE)) {
				SyncService.inst.forceSync();
				CommunicationService.inst.sendMessage(CommonPhraseHelper.DONE);
				return true;
			}
			SyncService.inst.exec();
			CommunicationService.inst.sendMessage(CommonPhraseHelper.DONE);
		} else {
			CommunicationService.inst.sendDefaultTextMessage("Sorry, the service is not running");
		}
		return true;
	}

	@Override
	public String getCommandText() {
		return "sync";
	}
}